package com.bsj.media.init;

import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yinhao
 * @version 1.0
 * @description 用于处理强制关闭直播
 * @date 2024/8/5
 */
@Slf4j
public class NettyLiveUDPIntercomUntil {
    private static Map<String, ChannelHandlerContext> channelMap = new ConcurrentHashMap<>();

    public static void push(String sim, ChannelHandlerContext channelHandlerContext) {
        ChannelHandlerContext channelHandlerContextNow = channelMap.get(sim);
        if (channelHandlerContextNow == null) {
            channelMap.put(sim, channelHandlerContext);
        } else {
            boolean active = channelHandlerContextNow.channel().isActive();
            //已经关闭链接
            if (!active) {
                log.warn("链路已经关闭:" + sim);
                channelMap.put(sim, channelHandlerContext);
            }
        }
    }

    public static ChannelHandlerContext gain(String sim) {
        return channelMap.get(sim);
    }

    public static void del(String sim) {
        ChannelHandlerContext remove = channelMap.remove(sim);
        if (remove != null) {
            remove.close();
        }
    }
}
